home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / d_d / caltech / inbound / jewelry.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-22  |  6.9 KB  |  269 lines

  1. /*    AD&D jewelry rolling program,
  2.  *        written August, 1992, by Ken Jenks, 
  3.  *        kjenks@gothamcity.jsc.nasa.gov
  4.  *
  5.  *    This program randomly determines the properties of non-magical
  6.  *      jewelry.
  7.  *
  8.  *    Compile and run with no command line options to see 'usage.'
  9.  */
  10.  
  11. #include <ctype.h>
  12. #include <math.h>
  13. #include <stdio.h>
  14.  
  15. static int
  16. bv_table[7][5] = {
  17.  /*  %     n   d     m  g  Jewelry value is n dice of size d, multiplied by m *
  18.   *---    --  --  ----  -  If g = 1, this piece has gems and may increase     */
  19.   { 10,   10, 10,    1, 0},  /* base value place 0 */
  20.   { 20,    2,  6,  100, 0},  /* base value place 1 */
  21.   { 40,    3,  6,  100, 0},  /* base value place 2 */
  22.   { 50,    5,  6,  100, 0},  /* base value place 3 */
  23.   { 70,    1,  6, 1000, 1},  /* base value place 4 */
  24.   { 90,    2,  4, 1000, 1},  /* base value place 5 */
  25.   {100,    2,  6, 1000, 1}}; /* base value place 6 */
  26.  
  27.  
  28. static char *
  29. descriptions[7] = {
  30.   "Ivory or wrought silver",          /* 0 */
  31.   "Wrought silver and gold",          /* 1 */
  32.   "Wrought gold",                     /* 2 */
  33.   "Jade, coral or wrought platinum",  /* 3 */
  34.   "Silver with gems",                 /* 4 */
  35.   "Gold with gems",                   /* 5 */
  36.   "Platinum with gems"};              /* 6 */
  37.  
  38. static char *
  39. kind[51] = {"anklet",
  40.             "arm band",
  41.             "belt",
  42.             "box(small)",
  43.             "bracelet",
  44.             "brooch",
  45.             "buckle",
  46.             "chain",
  47.             "chalice",
  48.             "choker",
  49.             "clasp",
  50.             "collar",
  51.             "comb",
  52.             "coronet",
  53.             "crown",
  54.             "decanter",
  55.             "diadem",
  56.             "earring",
  57.             "fob",
  58.             "goblet",
  59.             "headband(fillet)",
  60.             "idol",
  61.             "locket",
  62.             "medal",
  63.             "medallion",
  64.             "necklace",
  65.             "pendant",
  66.             "pin",
  67.             "orb",
  68.             "ring",
  69.             "sceptre",
  70.             "seal",
  71.             "statuette",
  72.             "tiara",
  73.  
  74.             "bracers",
  75.             "cup",
  76.             "dagger",
  77.             "flask",
  78.             "helm",
  79.             "horn",
  80.             "mirror",
  81.             "periapt",
  82.             "phylactery",
  83.             "rod",
  84.             "scarab",
  85.             "skull",
  86.             "shield",
  87.             "staff",
  88.             "sword",
  89.             "talisman",
  90.             "trident"};
  91.  
  92. /************************************************************************
  93. * die_roll() - simple function to roll one die of the given size
  94. ************************************************************************/
  95.  
  96. int die_roll(size)
  97. int size;
  98. {
  99.    return ((rand() % size) + 1);
  100. }
  101.  
  102. /************************************************************************
  103. * random_jewelry() - randomly determine jewelry
  104. *
  105. ************************************************************************/
  106.  
  107. void
  108. random_jewelry(num_jewelry, base_value, outfile)
  109. int num_jewelry, base_value;
  110. FILE *outfile;
  111. {
  112.   int i;
  113.   long bv, total = 0;
  114.   int bv_die;
  115.   int base_value_place;
  116.   int die;
  117.   int increase_from_gems;
  118.   char * nice_gems;
  119.   char * nice_work;
  120.  
  121.   fprintf(outfile,"%i piece%s of jewelry, ",
  122.     num_jewelry,
  123.     (num_jewelry!=1)?"s":"");
  124.  
  125.   if(base_value)
  126.     fprintf(outfile,"base value %i\n\n",base_value);
  127.   else
  128.     fprintf(outfile,"random base values\n\n");
  129.  
  130.   for (i=1; i<=num_jewelry; i++) {
  131.  
  132.     if(base_value) {
  133.       bv = base_value;
  134.       /* Calculate base_value_place for later, since no jewelry may *
  135.        * increase more than 7 places beyond its original base value  */
  136.       base_value_place=0;
  137.       while ((base_value_place<12)
  138.         && (base_value > bv_table[base_value_place][1]))
  139.         ++base_value_place;
  140.       if(base_value_place >= 6)
  141.         base_value_place = 5;
  142.  
  143.     } else {
  144.       bv_die = die_roll(100);
  145.       base_value_place=0;
  146.       while((base_value_place<6)
  147.         && (bv_die >= bv_table[base_value_place][0]))
  148.         base_value_place++;
  149.     }
  150.  
  151. #ifdef DEBUG
  152.   fprintf(stderr,"\nJewelry #%i\n\n",i);
  153. #endif
  154.  
  155.     nice_work = "";
  156.  
  157. roll_bv_dice:
  158.  
  159.     if(base_value)
  160.       bv = base_value;
  161.     else {
  162.       /* Next, roll the dice and determine bv */
  163.       bv = 0;
  164.       for(bv_die = 1; bv_die <= bv_table[base_value_place][1]; bv_die ++)
  165.         bv += die_roll(bv_table[base_value_place][2]) 
  166.             * bv_table[base_value_place][3];
  167.     }
  168.  
  169. #ifdef DEBUG
  170.   fprintf(stderr,"  base value: %i\n",bv);
  171. #endif
  172.  
  173. check_for_exceptional:
  174.  
  175. #ifdef DEBUG
  176.   fprintf(stderr,"  base value place: %i\n",base_value_place);
  177. #endif
  178.  
  179.     /* Check for exceptional workmanship */
  180.     if(die_roll(10) == 1) {
  181.       nice_work = " (good work)";
  182.  
  183. #ifdef DEBUG
  184.   fprintf(stderr,"  Exceptional workmanship: increase from %i\n",bv);
  185. #endif
  186.  
  187.       if(bv == bv_table[base_value_place][1] * bv_table[base_value_place][2] 
  188.           * bv_table[base_value_place][3])
  189.         if(base_value_place < 6) {
  190.           ++base_value_place;
  191.           goto roll_bv_dice;
  192.         }
  193.         else
  194.           ; /* If already at max, do notthing */
  195.       else {
  196.         /* Set bv to maximum for this base_value_place */
  197.         bv = bv_table[base_value_place][1] * bv_table[base_value_place][2] 
  198.           * bv_table[base_value_place][3];
  199.         goto check_for_exceptional;
  200.       }
  201.     }
  202.  
  203.     nice_gems = "";
  204.     increase_from_gems = 0;
  205.     if((bv_table[base_value_place][4]) && (die_roll(6) == 1)) {
  206.       increase_from_gems = 5000;
  207.       nice_gems = " (good gems)";
  208.       while((die_roll(6) == 1) && (increase_from_gems <= 200000))
  209.         increase_from_gems = increase_from_gems * 2;
  210.           
  211. #ifdef DEBUG
  212.   fprintf(stderr,"  gems increased value from %i to %i\n",
  213.        bv,bv+increase_from_gems);
  214. #endif
  215.     }
  216.  
  217.     bv += increase_from_gems;
  218.  
  219.     fprintf(outfile,"%3i  %7i  (%s) %s%s%s\n",
  220.       i,
  221.       bv, 
  222.       descriptions[base_value_place],
  223.       kind[die_roll(51)-1],
  224.       nice_gems,
  225.       nice_work);
  226.  
  227.     total += bv;
  228.  
  229.   } /* end for i */
  230.   fprintf(outfile,"\nTotal: %i\n",total);
  231.  
  232. }; /* end proc random_jewelry */
  233.  
  234. main(argc,argv)
  235. int argc;
  236. char **argv;
  237. {
  238.   int num_jewelry;
  239.   int base_value;
  240.  
  241.   if((argc != 2) && (argc != 3)) {
  242.     fprintf(stderr,"Randomly determines the properties of jewelry.\n");
  243.     fprintf(stderr,"\n");
  244.     fprintf(stderr," usage: %s number [base-value]\n",argv[0]);
  245.     fprintf(stderr,"   where 'number' is the number of pieces of jewelry desired\n");
  246.     fprintf(stderr,"         'base-value' is [optional] base value in GP\n");
  247.     fprintf(stderr,"           (default base-value is random)\n",
  248.              '%','%');
  249.     fprintf(stderr,"\n  Examples: %s 14       (generates 14 pieces of jewelry)\n",
  250.              argv[0]);
  251.     fprintf(stderr,"            %s 12 100   (generates 12 pieces of jewelry, base 100 GPV)\n",argv[0]);
  252.     exit(0);
  253.   }
  254.  
  255.   num_jewelry = atoi(argv[1]);
  256.  
  257.   if(argc==3)
  258.     base_value = atoi(argv[2]);
  259.   else
  260.     base_value = 0;
  261.  
  262.   /* seed random generator, may require system dependant command if
  263.    * time(0) does not work, or might need to use a scanf to get seed */
  264.   srand(time(0));
  265.  
  266.   random_jewelry(num_jewelry, base_value, stdout);
  267.  
  268. } /* end main */
  269.